package com.xdjd.mobile.business.util;

import com.gitee.easyopen.exception.ApiException;
import net.oschina.durcframework.easymybatis.PageInfo;
import net.oschina.durcframework.easymybatis.util.MyBeanUtil;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

public class CopyUtil {

	/**
	 * 拷贝全部属性
	 * 
	 * @param param
	 * @param clazz
	 * @return
	 */
	public static <T> T copyAll(Object param, Class<T> clazz) {
		if(param == null) {
			return null;
		}
		try {
			T obj = clazz.newInstance();
			BeanUtils.copyProperties(param, obj);
			return obj;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * 拷贝全部属性
	 * @param clazz 目标类
	 * @param obj 源数据
	 * @return
	 */
	public static <T> T copyAll(Class<T> clazz, Object... obj) {
		try {
			T info = clazz.newInstance();
			for (Object object : obj) {
				BeanUtils.copyProperties(object, info);
			}
			return info;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * 忽略null属性
	 * 
	 * @param param
	 * @param clazz
	 * @return
	 */
	public static <T> T copyIgnoreNull(Object param, Class<T> clazz) {
		try {
			T obj = clazz.newInstance();
			MyBeanUtil.copyProperties(param, obj);
			return obj;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * 拷贝一个List
	 * 
	 * @param oldList
	 * @param clazz
	 * @return
	 */
	public static <T> List<T> copyList(List<?> oldList, Class<T> clazz) {
		return ListUtil.copyList(oldList, clazz);
	}

	/**
	 * 拷贝一个PageInfo,替换List里面的元素
	 * 
	 * @param pageInfo
	 * @param retPojoClass
	 *            List里面的新元素
	 * @return 返回新的PageInfo
	 */
	public static <T> PageInfo<T> copyPageInfo(PageInfo<?> pageInfo, Class<T> retPojoClass) {
		return PageInfoUtil.copyPageInfo(pageInfo, retPojoClass);
	}
	
	/**
	 * 拷贝一个PageInfo,替换List里面的元素,指定PageInfo
	 * @param pageInfo
	 * @param retPojoClass List里面的新元素
	 * @param pageInfoClass PageInfo的class
	 * @return
	 */
	public static <T, P extends PageInfo<T>> PageInfo<T> copyPageInfo(PageInfo<?> pageInfo, Class<T> retPojoClass,Class<P> pageInfoClass) {
		List<?> list = pageInfo.getList();
		List<T> retList = new ArrayList<>(list.size());
		for (Object obj : list) {
			try {
				T resp = retPojoClass.newInstance();
				BeanUtils.copyProperties(obj, resp);
				retList.add(resp);
			} catch (Exception e) {
				throw new ApiException("拷贝失败");
			}
		}
		
		try {
			PageInfo<T> retPageInfo = pageInfoClass.newInstance();
			BeanUtils.copyProperties(pageInfo, retPageInfo);
			retPageInfo.setList(retList);

			return retPageInfo;
		} catch (Exception e) {
			throw new ApiException("拷贝失败");
		}
		
	}
	
	
	/**
	 * 拷贝一个PageInfo,替换List里面的元素
	 * 
	 * @param pageInfo
	 * @param retPojoClass
	 *            List里面的新元素
	 * @param copyFilter
	 *            过滤器
	 * @return 返回新的PageInfo
	 */
	public static <T> PageInfo<T> copyPageInfo(PageInfo<?> pageInfo, Class<T> retPojoClass,CopyFilter<T> copyFilter) {
		List<?> list = pageInfo.getList();
		List<T> retList = new ArrayList<>(list.size());
		for (Object obj : list) {
			try {
				T resp = retPojoClass.newInstance();
				BeanUtils.copyProperties(obj, resp);
				copyFilter.doFilter(obj, resp);
				retList.add(resp);
			} catch (Exception e) {
				throw new ApiException("拷贝失败");
			}
		}

		PageInfo<T> retPageInfo = new PageInfo<>();
		BeanUtils.copyProperties(pageInfo, retPageInfo);
		retPageInfo.setList(retList);

		return retPageInfo;
	}
	
	public static interface CopyFilter<T> {
		/**
		 * @param oldPojo 老对象
		 * @param t 新对象
		 */
		void doFilter(Object oldPojo, T t);
	}
}
